package com.shbaiche.bifenba.ui.my;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.base.Passport;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.user.UserInfoMessage;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseFragment;
import com.shbaiche.bifenba.util.MeiqiaUtils;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;
import com.shbaiche.bifenba.view.CircularImage;

import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 * Created by wanglei on 2016/12/10.
 */

public class MyFragment extends BaseFragment {
    @BindView(R.id.my_layout_info)
    LinearLayout layoutInfo;

    @BindView(R.id.my_btn_service)
    ImageView ivService;

    @BindView(R.id.my_iv_head)
    CircularImage ivHead;

    @BindView(R.id.my_tv_name)
    TextView tvName;

    @BindView(R.id.my_tv_login)
    TextView tvLogin;

    @BindView(R.id.my_lan_qq)
    TextView lanQQ;

    @BindView(R.id.my_zu_qq)
    TextView zuQQ;

    @BindView(R.id.my_lan_wx)
    TextView lanWX;

    @BindView(R.id.my_zu_wx)
    TextView zuWX;


    @BindView(R.id.my_btn_message)
    SuperTextView btnMessage;

    @BindView(R.id.my_btn_follow)
    SuperTextView btnFollow;

    @BindView(R.id.my_btn_collection)
    SuperTextView btnCollection;

    @BindView(R.id.my_btn_comment)
    SuperTextView btnComment;

    @BindView(R.id.my_btn_settings)
    SuperTextView btnSettings;

    public static MyFragment newInstance() {
        return new MyFragment();
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        layoutInfo.setOnClickListener(new DoClick());
        btnMessage.setOnClickListener(new DoClick());
        btnFollow.setOnClickListener(new DoClick());
        btnCollection.setOnClickListener(new DoClick());
        btnComment.setOnClickListener(new DoClick());
        btnSettings.setOnClickListener(new DoClick());
        ivService.setOnClickListener(new DoClick());

//        Passport p = new Passport();
//        p.setMobile("15999662463");
//        p.setToken("99661225ae34c4235f494d65d62b1b21");
//        p.setNickName("啊的撒啊");
//        p.setUserName("15999662463");
//        App.setPassport(p);

        setUserInfo();
    }

    @Override
    public void onResume() {
        super.onResume();
        setUserInfo();
    }

    private void setUserInfo() {
        if (App.getPassport() != null) {
            Passport passport = App.getPassport();
            try {
                if(tvName!=null){
                    if (passport.getNickName() != null) {
                        tvName.setText(passport.getNickName() + "");
                    } else if (passport.getMobile() != null) {
                        tvName.setText(passport.getMobile() + "");
                    } else {
                        tvName.setText("");
                    }
                    tvLogin.setVisibility(View.GONE);
                    Glide.with(this).load(passport.getPhotoPath()).placeholder(R.drawable.ic_my_head).error(R.drawable.ic_my_head).into(ivHead);
                    load();
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            if (tvName != null) {
                tvName.setText("登录");
                tvLogin.setVisibility(View.VISIBLE);
                ivHead.setImageResource(R.drawable.ic_my_head);
            }
        }
    }


    private void load() {

        UserInfoMessage msg = new UserInfoMessage();
        msg.setPassport(App.getPassport());
        NetApi.UserInfo(msg, new JsonCallback<TransMessage>() {

            @Override
            public void onFail(Call call, Exception e, int id) {

            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());
            }

            @Override
            public void onResponse(TransMessage response, int id) {
                Gson gson = new Gson();
                UserInfoMessage msg = gson.fromJson(response.getMessage(), UserInfoMessage.class);

                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {

                    Passport p = App.getPassport();
                    p.setNickName(msg.getNickName() + "");
                    p.setPhotoPath(msg.getPhoto());
                    App.setPassport(p);
                    tvName.setText(msg.getNickName() + "");
                    Glide.with(getActivity()).load(msg.getPhoto()).placeholder(R.drawable.ic_my_head).error(R.drawable.ic_my_head).into(ivHead);


                    // 微信足球群
                    List<String> wxFtGroup = msg.getWxFtGroup();
                    List<String> wxBkGroup = msg.getWxBkGroup();
                    List<String> qqFtGroup = msg.getQqFtGroup();
                    List<String> qqBkGroup = msg.getQqBkGroup();

                    if (wxFtGroup != null && wxFtGroup.size() > 0) {
                        zuWX.setText(wxFtGroup.get(0) + "");
                    }
                    if (wxBkGroup != null && wxBkGroup.size() > 0) {
                        lanWX.setText(wxBkGroup.get(0) + "");
                    }
                    if (qqFtGroup != null && qqFtGroup.size() > 0) {
                        zuQQ.setText(qqFtGroup.get(0) + "");
                    }
                    if (qqBkGroup != null && qqBkGroup.size() > 0) {
                        lanQQ.setText(qqBkGroup.get(0) + "");
                    }

                } else {
                }
            }
        });
    }

    private class DoClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.my_layout_info:
                    if (App.getPassport() == null) {
                        LoginActivity.launch(context);
                    } else {
                        SettingsActivity.launch(context);
                    }
                    break;
                case R.id.my_btn_message:
                    MessageListActivity.launch(context);
                    break;
                case R.id.my_btn_follow:
                    MyFollowActivity.launch(context);
                    break;
                case R.id.my_btn_collection:
                    MyCollectionActivity.launch(context);
                    break;
                case R.id.my_btn_comment:
                    MyCommentListActivity.launch(context);
                    break;
                case R.id.my_btn_settings:
                    SettingsActivity.launch(context);
                    break;
                case R.id.my_btn_service:
                    new MeiqiaUtils().openService(getActivity());
                    break;
            }
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_my;
    }
}
